{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a3e82827-7622-4038-be21-5f548612288f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import torch\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, GenerationConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4dcc3169-7a30-4441-a93c-1fd546c413fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e0504229a10149c48231707cb71d9659",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# This recipe requires a HuggingFace token to access the mistral models from the hub. You will need to create a HuggingFace login\n",
    "# and generate a token for use at https://huggingface.co/settings/tokens. You will also need to visit the model card at \n",
    "# https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.3 and accept the terms to use the model. Once you have generated the token,\n",
    "# save it in an environment variable named HUGGINGFACE_TOKEN and read it in the recipe as shown in the snippet below.\n",
    "from huggingface_hub import login\n",
    "hf_token = os.environ.get('HUGGINGFACE_TOKEN')\n",
    "login(token=hf_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eb43041e-5f81-4b31-b90f-257e39a3b4ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "746c5c098ad24aa6ac1fe0243221083c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/3 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "\n",
    "quantization_config = BitsAndBytesConfig(load_in_4bit=True,\n",
    "                                         bnb_4bit_compute_dtype=torch.bfloat16,\n",
    "                                         bnb_4bit_use_double_quant=True,\n",
    "                                         bnb_4bit_quant_type= \"nf4\"\n",
    "                                         )\n",
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "            \"mistralai/Mistral-7B-v0.3\", \n",
    "            device_map=\"auto\", \n",
    "            quantization_config=quantization_config,\n",
    "        )\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"mistralai/Mistral-7B-v0.3\", padding_side=\"left\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7c45b7fb-b33f-4d80-a36b-e2331b49ec03",
   "metadata": {},
   "outputs": [],
   "source": [
    "generation_config = GenerationConfig(\n",
    "    num_beams=4,\n",
    "    early_stopping=True,\n",
    "    eos_token_id=model.config.eos_token_id,\n",
    "    pad_token_id=model.config.eos_token_id,\n",
    "    max_new_tokens=900,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "02febbf4-ed87-455f-ac2d-b7b78aa3b32c",
   "metadata": {},
   "outputs": [],
   "source": [
    "seed_sentence = \"Step by step way on how to make an apple pie:\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "485356eb-adb1-48fd-ba14-aace7f937e10",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_inputs = tokenizer([seed_sentence], return_tensors=\"pt\").to(device)\n",
    "generated_ids = model.generate(**model_inputs, generation_config=generation_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "934d6293-f967-49eb-b374-be448ffedf1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Step by step way on how to make an apple pie:\n",
      "\n",
      "1. Preheat the oven to 350 degrees Fahrenheit.\n",
      "2. Peel and core the apples.\n",
      "3. Cut the apples into thin slices.\n",
      "4. Place the apples in a large bowl.\n",
      "5. Add the sugar, cinnamon, and nutmeg to the apples.\n",
      "6. Stir the apples until they are evenly coated with the sugar and spices.\n",
      "7. Pour the apples into a pie dish.\n",
      "8. Place the pie dish on a baking sheet.\n",
      "9. Bake the pie for 45 minutes to 1 hour, or until the apples are soft and the crust is golden brown.\n",
      "10. Remove the pie from the oven and let it cool for 10 minutes before serving.\n",
      "\n",
      "## How do you make an apple pie from scratch?\n",
      "\n",
      "To make an apple pie from scratch, you will need the following ingredients:\n",
      "\n",
      "- 2 cups of all-purpose flour\n",
      "- 1 teaspoon of salt\n",
      "- 1/2 cup of shortening\n",
      "- 1/2 cup of cold water\n",
      "- 4 cups of peeled, cored, and sliced apples\n",
      "- 1 cup of sugar\n",
      "- 1 teaspoon of cinnamon\n",
      "- 1/4 teaspoon of nutmeg\n",
      "- 1/4 teaspoon of allspice\n",
      "- 2 tablespoons of cornstarch\n",
      "- 1 tablespoon of lemon juice\n",
      "\n",
      "To make the pie crust, combine the flour and salt in a large bowl. Cut in the shortening with a pastry blender or two knives until the mixture resembles coarse crumbs. Add the cold water, 1 tablespoon at a time, until the dough comes together. Divide the dough in half and shape each half into a disk. Wrap the disks in plastic wrap and refrigerate for at least 30 minutes.\n",
      "\n",
      "To make the apple filling, combine the apples, sugar, cinnamon, nutmeg, allspice, cornstarch, and lemon juice in a large bowl. Toss to coat the apples evenly.\n",
      "\n",
      "Preheat the oven to 425 degrees Fahrenheit.\n",
      "\n",
      "Roll out one of the pie crusts on a lightly floured surface. Transfer the crust to a 9-inch pie plate. Spoon the apple filling into the crust.\n",
      "\n",
      "Roll out the second pie crust on a lightly floured surface. Cut the crust into strips. Arrange the strips in a lattice pattern on top of the apple filling.\n",
      "\n",
      "Bake the pie for 40 to 45 minutes, or until the crust is golden brown and the filling is bubbly. Cool the pie on a wire rack for at least 30 minutes before serving.\n",
      "\n",
      "## How do you make an apple pie from scratch step by step?\n",
      "\n",
      "To make an apple pie from scratch, you will need the following ingredients:\n",
      "\n",
      "- 2 cups of all-purpose flour\n",
      "- 1 teaspoon of salt\n",
      "- 1/2 cup of shortening\n",
      "- 1/2 cup of cold water\n",
      "- 4 cups of peeled, cored, and sliced apples\n",
      "- 1 cup of sugar\n",
      "- 1 teaspoon of cinnamon\n",
      "- 1/4 teaspoon of nutmeg\n",
      "- 1/4 teaspoon of allspice\n",
      "- 2 tablespoons of cornstarch\n",
      "- 1 tablespoon of lemon juice\n",
      "\n",
      "To make the pie crust, combine the flour and salt in a large bowl. Cut in the shortening with a pastry blender or two knives until the mixture resembles coarse crumbs. Add the cold water, 1 tablespoon at a time, until the dough comes together. Divide the dough in half and shape each half into a disk. Wrap the disks in plastic wrap and refrigerate for at least 30 minutes.\n",
      "\n",
      "To make the apple filling, combine the apples, sugar, cinnamon, nutmeg, allspice, cornstarch, and lemon juice in a large\n"
     ]
    }
   ],
   "source": [
    "generated_tokens = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]\n",
    "print(generated_tokens)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chapter10",
   "language": "python",
   "name": "chapter10"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
